import { numAddNoKeep } from '@/util'
import { ElMessage } from "element-plus"
import { numAdd, numSub, numMulti } from '@/util'
import { checkSingleRow } from '@/components/ScanBillForm/utils/index'
export function moneyParse(moneyStr) {
  console.log(moneyStr, 'moneyStr', typeof moneyStr)
  let num = typeof moneyStr == 'number' ? moneyStr : Number(moneyStr ? moneyStr.replace(/¥|￥/g, '') : moneyStr)
  return isNaN(num) ? 0 : num
}
const calcDetailAmount = (arr) => {
  let taxAmount = 0
  let excludingTaxAmount = 0
  let allAmount = 0
  arr.forEach(item => {
    taxAmount = numAddNoKeep(taxAmount, item.taxAmount)
    excludingTaxAmount = numAddNoKeep(excludingTaxAmount, item.excludingTaxAmount)
  })
  allAmount = numAddNoKeep(taxAmount, excludingTaxAmount)
  return {
    taxAmount,
    excludingTaxAmount,
    allAmount
  }
}
const checkNumber = (value) => {
  if(typeof value !== 'number' && !value){
    return true
  }
  return false
}
const moneyValidate = (item, detailList, totalKey = '小写金额') => {
  // 验证明细行
  let detailCheck = true
  console.log(detailList, 'detailList', detailList.length)
  if (!detailList || !detailList.length || detailList.length <= 0) {
    detailCheck = false
  }
  detailList && detailList.forEach(item => {
    if (checkNumber(item.goodsName) || checkNumber(item.taxAmount) || checkNumber(item.excludingTaxAmount) || checkNumber(item.taxRate)) {
      detailCheck = false
    }
  })
  if (!detailCheck) {
    ElMessage({
      type: "error",
      message: '请完善明细行信息！',
    })
    return false
  }
  let rowTaxCheck = true
  if (detailList) {
    detailList.forEach(rowItem => {
      if (checkSingleRow(rowItem)) {
        rowTaxCheck = false
      }
    })
  }
  if (!rowTaxCheck) {
    ElMessage({
      type: "error",
      message: '发票明细行的不含税金额、税率或税额识别异常，请检查！',
    })
    return false
  }
  console.log(detailList, 'detailList')
  if(moneyParse(item[totalKey]) === numAddNoKeep(moneyParse(item['合计金额']), moneyParse(item['合计税额']))){
    console.log('这里', calcDetailAmount(detailList), moneyParse(item[totalKey]), item, totalKey, item[totalKey])
    let detailAmountObj = calcDetailAmount(detailList)
    if (detailAmountObj.allAmount != moneyParse(item[totalKey])) {
      ElMessage({
        type: "error",
        message: '明细价税合计金额不等于发票的价税合计金额',
      })
      return false
    } else if (detailAmountObj.excludingTaxAmount != moneyParse(item['合计金额'])) {
      ElMessage({
        type: "error",
        message: '明细的不含税金额合计不等于发票的不含税金额合计',
      })
      return false
    } else if (detailAmountObj.taxAmount != moneyParse(item['合计税额'])) {
      ElMessage({
        type: "error",
        message: '明细的税额合计不等于发票的税额合计',
      })
      return false
    } else {
      return true
    }
  }else{
    ElMessage({
      type: "error",
      message: '请验证价税合计金额是否等于合计金额+合计税额',
    })
    return false
  }
}
export const typeOptions = {
    31: {
        type: 31,
        details: true,
        label: "增值税专用发票",
        getinvoiceAmount(item){
            return item['小写金额']
        },
        getinvoiceNo(item){
            return item['发票号码']
        },
        getinvoiceCode(item){
            return item['发票代码']
        },
        getinvoiceDate(item){
            return item['开票日期']
        },
        Validity(item, detailList){
          return moneyValidate(item, detailList)
        }
    },
    32: {
        type: 32,
        label: "增值税电子专用发票",
        details: true,
        getinvoiceAmount(item){
            return item['小写金额']
        },
        getinvoiceNo(item){
            return item['发票号码']
        },
        getinvoiceCode(item){
            return item['发票代码']
        },
        getinvoiceDate(item){
            return item['开票日期']
        },
        Validity(item, detailList){
          return moneyValidate(item, detailList)
        }
    },
    33: {
        type: 33,
        details: true,
        label: "增值税普通发票",
        getinvoiceAmount(item){
            return item['小写金额']
        },
        getinvoiceNo(item){
            return item['发票号码']
        },
        getinvoiceCode(item){
            return item['发票代码']
        },
        getinvoiceDate(item){
            return item['开票日期']
        },
        Validity(item, detailList){
          return moneyValidate(item, detailList)
        }
    },
    34: {
        type: 34,
        details: true,
        label: "增值税电子普通发票",
        getinvoiceAmount(item){
            return item['小写金额']
        },
        getinvoiceNo(item){
            return item['发票号码']
        },
        getinvoiceCode(item){
            return item['发票代码']
        },
        getinvoiceDate(item){
            return item['开票日期']
        },
        Validity(item, detailList){
          return moneyValidate(item, detailList)
        }
    },
    40: {
      type: 40,
      details: true,
      label: "电子发票(普通发票)",
      getinvoiceAmount(item){
          return item['价税合计(小写)']
      },
      getinvoiceNo(item){
          return item['发票号码']
      },
      getinvoiceCode(item){
          return item['发票代码']
      },
      getinvoiceDate(item){
          return item['开票日期']
      },
      Validity(item, detailList){
        return moneyValidate(item, detailList, '价税合计(小写)')
      }
    },
    41: {
      type: 41,
      details: true,
      label: "电子发票(增值税专用发票)",
      getinvoiceAmount(item){
          return item['价税合计(小写)']
      },
      getinvoiceNo(item){
          return item['发票号码']
      },
      getinvoiceCode(item){
          return item['发票代码']
      },
      getinvoiceDate(item){
          return item['开票日期']
      },
      Validity(item, detailList){
        return moneyValidate(item, detailList, '价税合计(小写)')
      }
    },
    11: {
        type: 11,
        label: "增值税普通发票（卷式）",
        getinvoiceAmount(item){
            return item['合计金额(小写)']
        },
        getinvoiceNo(item){
            return item['发票号码']
        },
        getinvoiceCode(item){
            return item['发票代码']
        },
        getinvoiceDate(item){
            return item['开票日期']
        },
        Validity(item){
            return true
        }
    },
    2: {
        type: 2,
        label: "铁路行业发票(火车票)",
        getinvoiceAmount(item){
            return item['票价']
        },
        getinvoiceNo(item){
            return item['编号']
        },
        getinvoiceCode(item){
            return ''
        },
        getinvoiceDate(item){
            return item['出发时间']
        },
        Validity(item){
            return true
        }
    },
    5: {
        type: 5,
        label: "航空运输电子客票行程单(机票行程单)",
        getinvoiceAmount(item){
            return numAdd(numAdd(item['票价'] || 0, item['燃油附加费'] || 0), item['民航发展基金'] || 0)
        },
        getinvoiceNo(item){
            return item['电子客票号码']
        },
        getinvoiceCode(item){
            return ''
        },
        getinvoiceDate(item){
            return item['开票日期']
        },
        Validity(item){
          if (item['合计金额'] == numAdd(numAdd(item['票价'], item['民航发展基金']), item['燃油附加费'])) {
            return true
          } else {
            ElMessage({
              type: "error",
              message: '请验证合计金额是否等于票价+民航发展基金+燃油附加费',
            })
          }
        }
    },
    0: {
        type: 0,
        label: "出租车发票",
        getinvoiceAmount(item){
            return item['金额']
        },
        getinvoiceNo(item){
            return item['发票号码']
        },
        getinvoiceCode(item){
            return item['发票代码']
        },
        getinvoiceDate(item){
            return item['日期']
        },
        Validity(item){
            return true
        }
    },
    35: {
        type: 35,
        details: true,
        label: "增值税电子普通发票（通行费）",
        getinvoiceAmount(item){
            return item['小写金额']
        },
        getinvoiceNo(item){
            return item['发票号码']
        },
        getinvoiceCode(item){
            return item['发票代码']
        },
        getinvoiceDate(item){
            return item['开票日期']
        },
        Validity(item, detailList){
          return moneyValidate(item, detailList)
        }
    },
    1: {
        type: 1,
        label: "定额发票",
        getinvoiceAmount(item){
            return item['小写金额']
        },
        getinvoiceNo(item){
            return item['发票号码']
        },
        getinvoiceCode(item){
            return item['发票代码']
        },
        getinvoiceDate(item){
            return item['开票日期']
        },
        Validity(item){
            return true
        }
    },
    121: {
        type: 121,
        label: "机动车销售统一发票(购车发票)",
        getinvoiceAmount(item){
            return item['价税合计(小写)']
        },
        getinvoiceNo(item){
            return item['发票号码']
        },
        getinvoiceCode(item){
            return item['发票代码']
        },
        getinvoiceDate(item){
            return item['开票日期']
        },
        Validity(item){
          if (moneyParse(item['价税合计(小写)']) === numAddNoKeep(moneyParse(item['不含税价(小写)']), moneyParse(item['增值税税额']))) {
            return true
          } else {
            ElMessage({
              type: "error",
              message: '请验证价税合计金额是否等于合计金额+合计税额',
            })
          }
        }
    },
    122: {
        type: 122,
        label: "二手车销售发票(购车发票)",
        getinvoiceAmount(item){
            return item['车价合计（小写）']
        },
        getinvoiceNo(item){
            return item['发票号码']
        },
        getinvoiceCode(item){
            return item['发票代码']
        },
        getinvoiceDate(item){
            return item['开票日期']
        },
        Validity(item){
            return true
        }
    },
    8: {
        type: 8,
        label: "通用机打发票",
        getinvoiceAmount(item){
            return item['合计金额(小写)']
        },
        getinvoiceNo(item){
            return item['发票号码']
        },
        getinvoiceCode(item){
            return item['发票代码']
        },
        getinvoiceDate(item){
            return item['开票日期']
        },
        Validity(item){
            return true
        }
    },
    9: {
        type: 9,
        label: "汽车票",
        getinvoiceAmount(item){
            return item['票价']
        },
        getinvoiceNo(item){
            return item['发票号码']
        },
        getinvoiceCode(item){
            return item['发票代码']
        },
        getinvoiceDate(item){
            return item['开票日期']
        },
        Validity(item){
            return true
        }
    },
    10: {
        type: 10,
        label: "轮船票",
        getinvoiceAmount(item){
          console.log(item, '@@@')
          return item['票价']
        },
        getinvoiceNo(item){
            return item['发票号码']
        },
        getinvoiceCode(item){
            return item['发票代码']
        },
        getinvoiceDate(item){
            return item['日期']
        },
        Validity(item){
            return true
        }
    },
    13: {
        type: 13,
        label: "过路过桥费发票",
        getinvoiceAmount(item){
            return item['金额']
        },
        getinvoiceNo(item){
            return item['发票号码']
        },
        getinvoiceCode(item){
            return item['发票代码']
        },
        getinvoiceDate(item){
            return item['日期']
        },
        Validity(item){
            return true
        }
    },
    20: {
        type: 20,
        label: "其他票据",
        getinvoiceAmount(item){
            return item['价税合计金额']
        },
        getinvoiceNo(item){
            return item['发票号码']
        },
        getinvoiceCode(item){
            return item['发票代码']
        },
        getinvoiceDate(item){
            return item['开票日期']
        },
        Validity(item){
          console.log(item['价税合计金额'], 'd', item['金额合计(不含税)'], 'w', item['税额合计'], numAddNoKeep(moneyParse(item['金额合计(不含税)']), moneyParse(item['税额合计'])))
            if(item['价税合计金额'] && item['金额合计(不含税)'] && item['税额合计']){
                if(moneyParse(item['价税合计金额']) === numAddNoKeep(moneyParse(item['金额合计(不含税)']), moneyParse(item['税额合计']))){
                  return true
                }else{
                  ElMessage({
                    type: "error",
                    message: '请验证价税合计金额是否等于合计金额+合计税额',
                  })
                  return false
                }
            }else{
              return true
            }
        }
    }
}